const http=require('http');
const path=require('path');
const fs=require('fs');

http.createServer((req,res)=>{
    var urlObj=new URL(req.url,'http://localhost:8083');
    var pathName=urlObj.pathname;
    if(pathName.indexOf('/detail')==0){
        var chapterPath=path.join(__dirname,'/chapter.html');
        var chapterContent=fs.readFileSync(chapterPath);
        res.writeHead(200,{'content-type':'text/html;charset=utf-8'});
        res.write(chapterContent);
        res.end();
    }else if(pathName.indexOf('/css/')==0){
        var fileName=pathName.slice(5);
        var filePath=path.join(__dirname,`/css/${fileName}`);
        var fileContent=fs.readFileSync(filePath);
        res.writeHead(200,{'content-type':'text/css'});
        res.write(fileContent);
        res.end();
    }else if(pathName.indexOf('/images/')==0){
        var fileName=pathName.slice(8);
        var filePath=path.join(__dirname,`/images/${fileName}`);
        var fileContent=fs.readFileSync(filePath);
        res.writeHead(200,{'content-type':'image/jpeg'});
        res.write(fileContent);
        res.end();
    }
}).listen(8083);